#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <stdatomic.h>
#include "somb.h"

void test2_client_listen_method_onvailable_cb(uint32_t ServiceId,bool bAvailable)
{
    printf("test2 client listen method callback, recv serviceid=%08x is onvailbe=%d\n",ServiceId,bAvailable);

}

void test2_client_listen_event_cb(uint32_t ServiceId,void*EvtData,uint32_t EvtDataLen)
{
    printf("test2 client listen event callback, recv serviceid=%08x EvtDataLen=%d\n",ServiceId,EvtDataLen);
    printf("event data [%s]\n", (char*)EvtData);
}

static ST_SOMB_SERVICE_LISTEN_INFO stEventListenInfoList[]=
{
    {
        .ServiceId= 0x01010000,
        .ServiceType = SOMB_SVC_TYPE_METHOD,
        .OnAvailableCB = test2_client_listen_method_onvailable_cb,
    },
    {
        .ServiceId= 0x01010001,
        .ServiceType = SOMB_SVC_TYPE_EVENT,
        .ListenerFlags = SOMB_SVC_FLAGS_EVENT_LISTEN_SYNC_DATA, //this flags means the event data will sync to you when it is deployed by it's provider
        .ListenerCB = test2_client_listen_event_cb,
    },
    {
        .ServiceId = 0x01010002,
        .ServiceType = SOMB_SVC_TYPE_METHOD,
        .OnAvailableCB = test2_client_listen_method_onvailable_cb,
    },
    {
        .ServiceId= 0x020100F0,
        .ServiceType = SOMB_SVC_TYPE_METHOD,
        .OnAvailableCB = test2_client_listen_method_onvailable_cb,
    },
    {
        .ServiceId = 0x020100F1,
        .ServiceType = SOMB_SVC_TYPE_EVENT,
        .ListenerFlags = SOMB_SVC_FLAGS_EVENT_LISTEN_SYNC_DATA, //this flags means the event data will sync to you when it is deployed by it's provider
        .ListenerCB = test2_client_listen_event_cb,
    },
};
void add_listen_service()
{
    int32_t ret;

    printf("add listener on service test, arrsize=%ld\n",ARRAY_SIZE(stEventListenInfoList));
    ret = i32SOMB_ServiceListener_Add(stEventListenInfoList,ARRAY_SIZE(stEventListenInfoList));
    if(0 != ret)
    {
        printf("test2 event listener add failed\n");
    }
    else
    {
        printf("test2 event listener add success\n");
    }
}
void del_listen_service()
{
    int32_t ret;

    int arrsize = ARRAY_SIZE(stEventListenInfoList);
    uint32_t *EventServiceIdList = malloc(sizeof(uint32_t) * arrsize);

    for(int i=0; i < arrsize; i++)
    {
        EventServiceIdList[i] = stEventListenInfoList[i].ServiceId;
    }

    ret = i32SOMB_EventListener_Delete(EventServiceIdList,arrsize);
    if(0 != ret)
    {
        printf("test2 client listener delete failed\n");
    }
    else
    {
        printf("test2 client listener delete success\n");
    }

    free(EventServiceIdList);

    return ;
}

void print_usage(void)
{
    printf("1: add listen service\n");
    printf("2: del listen service\n");
}
int main(void)
{
    uint32_t SubNsId;
    int32_t ret = i32SOMB_SubNs_Init(&SubNsId);
    if(SOMB_OK != ret)
    {
        printf("consumer somb sub ns init failed\n");
        return -1;
    }
    printf("SubNs=%d SOMB Init success\n",SubNsId);

    while(1)
    {
        print_usage();
        printf("chose your test index:\n");
        int cmd;
        scanf("%d",&cmd);
        printf("your chose is %d \n",cmd);
        switch(cmd)
        {
            case 1: /*listen service*/
                add_listen_service();
                break;
            case 2:
                del_listen_service();
                break;
            default:
                break;
        }

    }

    return 0;
}
